import {ref} from 'vue'
import request from '@/base/request'
import User from '@/types/user'
import { ElMessage } from 'element-plus'
import Page from '@/base/page'
import { AxiosError } from 'axios'
export default function(){

    interface problemNumber{
        solveNumber:number,
        totalNumber:number
    }

    interface UserProblemCollect{
        problemId:number,
        problemDescribe:string
    }

    const userInfoParm=ref()

    const userProblemCollectList=ref<Page<UserProblemCollect>>({
            items:[],
            counts:1,
            page:1,
            pageSize:3
        })

    const userProblemCollectParm=ref({
        pageNo:1,
        pageSize:3,
    })

    const userInfo=ref<User>()

    const problemNumber=ref<Array<problemNumber>>([])

    



    async function getUserInfo(userId){
        const response=(await request.get(`/user/getUserInfo/${userId}`)).data
        if(response.code ===200){
            userInfo.value=response.data
            userInfoParm.value=JSON.parse(JSON.stringify(response.data));

            
        }else{
            ElMessage.error(`获取用户信息错误:${response.msg}，请稍后重试`)
        }
        
    }

    
    async function getProblemNumberByDifficulty(userId) {
        const response=(await request.get(`/problem/getProblemNumberByDiff/${userId}`)).data
        if(response.code===200){
            problemNumber.value=response.data
        }else{
            ElMessage.error(`获取用户错误:${response.msg}`)
        }
    }

    async function getUserProblemCollect() {
        const response = (await request.get(`/user/getUserProblemCollect/${userProblemCollectParm.value.pageNo}/${userProblemCollectParm.value.pageSize}`)).data
        if(response.code===200){
            userProblemCollectList.value=response.data
        }else{
            ElMessage.error(`获取收藏失败,${response.msg}`)
        }
    }

    async function addUserProblemCollect(problemId:number,problemDescribe:string) {
        const userProblemCollect={
            problemId:problemId,
            problemDescribe:problemDescribe
        }
        const response=(await request.post("/user/addUserProblemCollect",userProblemCollect)).data
        if(response.code===200){
            ElMessage.success("收藏成功");
        }else{
            ElMessage.error(`收藏失败:${response.msg}`)
            throw new AxiosError(response.msg)
        }
    }

    async function removeUserProblemCollect(problemId:number) {
        const response=(await request.delete(`/user/deleteUserProblemCollect/${problemId}`)).data
        if(response.code===200){
            
        }else{
            ElMessage.error(`取消失败,${response.msg}`)
            throw new AxiosError(response.msg)
        }
    }

    async function updateUser() {
        console.log(userInfoParm.value)
        const response=(await request.put("/user/updateUserInfo",userInfoParm.value)).data
        if(response.code===200){
            ElMessage.success('修改成功')
        }else{
            ElMessage.error(`修改失败,${response.msg}`)
        }

    }

    async function uploadAvatar(file) {
        const formData=new FormData();
        formData.append('file',file);
        const response=(await request.post('/user/resource/uploadAvatar',formData,{
            headers:{
                'Content-Type': 'multipart/form-data'
            }
        })).data

        if(response.code===200){
            userInfoParm.value.avatar=response.data
        }else{
            ElMessage.error(`上传头像失败：${response.msg}`)
        }

        return false;
    }
    

    

    return {userInfo,problemNumber,userProblemCollectList,userProblemCollectParm,userInfoParm,
        uploadAvatar,updateUser,addUserProblemCollect,removeUserProblemCollect,getUserProblemCollect,getProblemNumberByDifficulty,getUserInfo}
}